package com.loe.ui.api

import android.webkit.JavascriptInterface
import android.widget.ImageView
import com.loe.ui.*
import com.quickjs.JSObject

class ImageViewApi(private val view: ImageView) : ViewApi(view)
{
    @JavascriptInterface
    fun setColorFilter(s: String)
    {
        view.setColorFilter(s.color)
    }

    @JavascriptInterface
    fun setCropSrc(s: String, xRate: Double, yRate: Double, wRate: Double, hRate: Double)
    {
        s.preLoadBitmap(view.context)
        {
            view.setImageBitmap(it.crop(xRate,yRate, wRate, hRate))
        }
    }

    init
    {
        jsObject.registerFun("setSrc")
        {
            val o = it.get(0)
            if (o is JSObject)
            {
                view.setImageDrawable(o.shape)
            } else
            {
                view.src = o.toString()
            }
        }
    }
}